﻿<?php
function _autoFixSeoCategory($name, $category_id = 0){
	global $db;
    $keyword = seo_url($name);		
    $db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'category_id=" . (int)$category_id. "'");
		
    $tempquery = $db->query("Select COUNT(*) as total FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $keyword . "'")->row['total'];
	while($tempquery > 0){
		$keyword .= $tempquery;
		$tempquery = $db->query("Select COUNT(*) as total FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $keyword . "'")->row['total'];
	}
	return $keyword;
}
function addCategory($name, $href, $parent_id = 0, $sort_order) {
	global $db;
	$db->query("INSERT INTO " . DB_PREFIX . "category SET parent_id = '" . (int)$parent_id . "', `top` = '0', `column` = '1', sort_order = '" . (int)$sort_order . "', status = '1', date_modified = NOW(), date_added = NOW(), href = '" . $db->escape($href) . "'");
	$category_id = $db->getLastId();
		
	$keyword = _autoFixSeoCategory($name, $category_id);
	
	$db->query("INSERT INTO " . DB_PREFIX . "category_description SET category_id = '" . (int)$category_id . "', language_id = '2', name = '" . $db->escape($name) . "', meta_keyword = '" . $db->escape($name) . "', meta_description = '" . $db->escape($name) . "', description = '" . $db->escape($name) . "'");
	
	if ($keyword) {
		$db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $db->escape($keyword) . "'");
	}
	return $category_id;
}

function _autoFixSeoProduct($name, $product_id = 0){
	global $db;
    $keyword = seo_url($name);		
    $db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
		
    $tempquery = $db->query("Select COUNT(*) as total FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $keyword . "'")->row['total'];
	while($tempquery > 0){
		$keyword .= $tempquery;
		$tempquery = $db->query("Select COUNT(*) as total FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $keyword . "'")->row['total'];
	}
	return $keyword;
}

function addProduct($name, $model, $href, $catId = array()) {
	global $db;
	$db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $db->escape($model) . "', status = '1', date_added = NOW(), date_modified = NOW(), href = '" . $db->escape($href) . "'");
	
	$product_id = $db->getLastId();
		
	$keyword = _autoFixSeoProduct($name, $product_id);
	
	$db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '1', name = '" . $db->escape($name) . "', meta_keyword = '" . $db->escape($name) . "', meta_description = '" . $db->escape($name) . "', description = '" . $db->escape($name) . "'");
	
	$db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '0'");
	
	foreach ($catId as $category_id) {
		$db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
	}
	
	if ($keyword) {
		$db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $db->escape($keyword) . "'");
	}
	return $product_id;
}

function getProduct($product_id) {
	global $db;
	$query = $db->query("SELECT p.product_id, p.href, p.model, pd.name FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON(p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' ORDER BY p.product_id ASC");
	return $query->row;
}

function getProductByData($data) {
	global $db;
	$query = $db->query("SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON(p.product_id = pd.product_id) WHERE p.product_id <> '" . (int)$data['product_id'] . "' AND p.product_id > '" . (int)$data['product_id'] . "' AND LCASE(p.model) LIKE '" . $db->escape(utf8_strtolower($data['model'])) . "' AND LCASE(p.href) LIKE '" . $db->escape(utf8_strtolower($data['href'])) . "' AND LCASE(pd.name) LIKE '" . $db->escape(utf8_strtolower($data['name'])) . "'");
	
	$data = array();
	
	if($query->rows) {
		foreach($query->rows as $result) {
			$data[] = $result['product_id'];
		}
	}
	return $data;
}

function getProducts($start, $limit) {
	global $db;
	$query = $db->query("SELECT p.product_id, p.href FROM " . DB_PREFIX . "product p ORDER BY p.product_id ASC LIMIT " . $start . ", " . $limit);
	return $query->rows;
}

function editProduct($product_id, $image, $price, $special, $description, $meta_keyword, $meta_description) {
	global $db;
	if ($image) {
		$db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $db->escape(html_entity_decode($image, ENT_QUOTES, 'UTF-8')) . "', date_available = '2012-08-01' WHERE product_id = '" . (int)$product_id . "'");
	}
	
	if ($price) {
		$db->query("UPDATE " . DB_PREFIX . "product SET price = '" . (float)$price . "' WHERE product_id = '" . (int)$product_id . "'");
	}
	if ($special) {
		$db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
		$db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '0', priority = '0', price = '" . (float)$special . "', date_start = '0000-00-00 00:00:00', date_end = '0000-00-00 00:00:00'");
	}
	
	if ($description) {
		$db->query("UPDATE " . DB_PREFIX . "product_description SET meta_keyword = '" . $db->escape($meta_keyword) . "', meta_description = '" . $db->escape($meta_description) . "', description = '" . $db->escape($description) . "' WHERE product_id = '" . (int)$product_id . "'");
	}	
}

function deleteProduct($product_id) {
	global $db;
	$db->query("DELETE FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
	$db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
	$db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
	$db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
	$db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
	$db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
}
?>